package eln.zaislu.muziejus;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;

public class AnimatedActor extends Actor {
	 
	   
		private static final String TAG = AnimatedActor.class.getSimpleName();
	 
	    private Rect sourceRect;    // the rectangle to be drawn from the animation bitmap
	    private int frameNr;        // number of frames in animation
	    private int currentFrame;   // the current frame
	    private long frameTicker;   // the time of the last frame update
	    private int framePeriod;    // milliseconds between each frame (1000/fps)
	 
	    private int spriteWidth;    // the width of the sprite to calculate the cut out rectangle
	    private int spriteHeight;   // the height of the sprite
	 	    
	    public AnimatedActor(Bitmap bitmap, int x, int y, int width, int height, int fps, int frameCount,int objid, int soundid,int animationOrientation) {
	    	          super(bitmap, x, y,true,false,objid,soundid,(int)(width/frameCount*0.66666f),(int)(height*0.66666f),animationOrientation);
	    		        currentFrame = 0;
	    		        frameNr = frameCount;
	    		        spriteWidth = (int)(width/frameCount*0.66666f);
	    		        spriteHeight = (int)(height*0.66666f);
	    		        sourceRect = new Rect(0, 0, spriteWidth, spriteHeight);
	    		        framePeriod = 1000 / fps;
	    		        frameTicker = 0l;
	    		    }
	    
	 
	  public Rect getSourceRect() {
			return sourceRect;
		}
		public void setSourceRect(Rect sourceRect) {
			this.sourceRect = sourceRect;
		}
		public int getFrameNr() {
			return frameNr;
		}
		public void setFrameNr(int frameNr) {
			this.frameNr = frameNr;
		}
		public int getCurrentFrame() {
			return currentFrame;
		}
		public void setCurrentFrame(int currentFrame) {
			this.currentFrame = currentFrame;
		}
		public int getFramePeriod() {
			return framePeriod;
		}
		public void setFramePeriod(int framePeriod) {
			this.framePeriod = framePeriod;
		}
		public int getSpriteWidth() {
			return spriteWidth;
		}
		public void setSpriteWidth(int spriteWidth) {
			this.spriteWidth = spriteWidth;
		}
		public int getSpriteHeight() {
			return spriteHeight;
		}
		public void setSpriteHeight(int spriteHeight) {
			this.spriteHeight = spriteHeight;
		}
		
		// the update method for Elaine
		public void update(long gameTime) {
			if (gameTime > frameTicker + framePeriod) {
				frameTicker = gameTime;
				// increment the frame
				currentFrame++;
				if ((currentFrame >= frameNr) && this.isAnimationLooped()){
					currentFrame = 0;
				}else if (!this.isAnimationLooped()){
					if (currentFrame >= frameNr-1){
					   currentFrame=frameNr-1;
					}
				}
			}
			// define the rectangle to cut out sprite
			this.sourceRect.left = currentFrame * spriteWidth;
			this.sourceRect.right = this.sourceRect.left + spriteWidth;
		}
		
		// the draw method which draws the corresponding frame
		public void draw(Canvas canvas) {
		
			Rect destRect = new Rect(getX()- (spriteWidth / 2), getY()- (spriteHeight / 2), getX() + spriteWidth/2, getY() + spriteHeight/2);
			//Paint pn=new Paint();
			//pn.setColor(-65536);
			//canvas.drawRect(destRect, pn);
			canvas.drawBitmap(bitmap, sourceRect, destRect, null);
		}
		
	}
